<template>
  <div class="searchResults">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync="error"
  error-text="请求失败，点击重新加载"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSeachResult } from '@/api/search.js'
export default {
  props: {
    seatchvalue: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      error: false
    }
  },

  created () {},

  methods: {
    async  onLoad () {
      //  发请求
      try {
        const res = await getSeachResult({
          page: this.page,
          per_page: 10,
          q: this.seatchvalue
        })

        // 出错
        if (Math.random() <= 0.5) {
          console.lg(11)
        }
        // 2获取数据保存
        this.list.push(...res.results)
        //  3加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (!res.results.length) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        // 显示错误
        this.error = true
        // 出现错误让加载关闭
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
